package com.syqc.monitor.ui.newaddcar

import com.syqc.comlib.utils.GsonUtil
import com.syqc.entity.VideoPass
import com.syqc.entity.Videos
import com.syqc.entity.VideosPass

/**
 *@author  hfjs
 *@time   2024/8/27 10:29
 *@desc
 */
object VideoPassUtil {
    private val passList = mutableListOf<VideoPass>()

    private val keys = mutableListOf<Int>()
    private val hasKeys = mutableListOf<Int>()

    init {
        for (i in 1..32) {
            keys.add(i)
        }
        keys.add(100)
        keys.add(101)
        keys.add(103)
    }

    //设置多少个视频逻辑通道
    fun setPassNum(num: Int): MutableList<VideoPass> {
        passList.clear()
        hasKeys.clear()
        for (i in 1..num) {
            hasKeys.add(i)
            passList.add(VideoPass(i))
        }
        return passList
    }

    //设置逻辑通道
    fun setPass(index: Int, key: Int, newKey: Int) {
        val videoPass = passList[index]
        videoPass.pass = newKey
        hasKeys.remove(key)
        hasKeys.add(newKey)
    }

    //设置逻辑通道名称
    fun setPassName(index: Int, passName: String) {
        passList[index].passName = passName
    }

    //获取没有选中的逻辑通道号
    fun getNoChooseKey(): MutableList<Int> {
        val list = mutableListOf<Int>()
        keys.forEach {
            if (!hasKeys.contains(it)) list.add(it)
        }
        return list
    }

    //处理视频通道
    fun handlePass(videos: String?): MutableList<VideoPass> {
        hasKeys.clear()
        passList.clear()

        val entity = GsonUtil.formObject<VideosPass>(videos)
        if (entity.vs == null) return passList
        val passArray = entity.vs.split(",")
        passArray.forEach {
            val infoArray = it.split(":")
            val passNum = infoArray[0].toInt()
            val passName = infoArray[1]
            val cloud = if (infoArray.size > 2) infoArray[2] else "0"
            hasKeys.add(passNum)
            passList.add(VideoPass(passNum, passName, cloud))
        }
        return passList
    }


    //获取通道信息
    fun getVideoPass(list: MutableList<VideoPass>?): String? {
        if (list.isNullOrEmpty()) return null
        var passValue = ""
        for (pass in list) {
            passValue = "$passValue,${pass.pass}:${pass.passName}:${pass.cloud}"
        }
        passValue = passValue.substring(1)
        return GsonUtil.toJson(Videos(passValue))
    }
}